package com.smart.manager.work.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.ObjUtil;
import com.smart.common.entities.constant.ArcoTips;
import com.smart.common.entities.core.Page;
import com.smart.common.entities.domain.AclMeta;
import com.smart.common.entities.option.SelectOption;
import com.smart.manager.system.entities.views.SimpleAccount;
import com.smart.manager.system.entities.views.query.QSimpleAccount;
import com.smart.manager.work.entities.assembler.TasksList;
import com.smart.manager.work.entities.body.RTaskMain;
import com.smart.manager.work.entities.enums.PigOrderStatus;
import com.smart.manager.work.entities.models.BasicStaff;
import com.smart.manager.work.entities.models.PigOrder;
import com.smart.manager.work.entities.models.TaskKind;
import com.smart.manager.work.entities.models.TaskMain;
import com.smart.manager.work.entities.models.query.QBasicStaff;
import com.smart.manager.work.entities.models.query.QPigOrder;
import com.smart.manager.work.entities.models.query.QTaskKind;
import com.smart.manager.work.entities.models.query.QTaskMain;
import com.smart.manager.work.views.BasicStaffView;
import com.smart.manager.work.views.OrderView;
import com.smart.manager.work.views.query.QBasicStaffView;
import com.smart.utils.AclUtils;
import io.ebean.DB;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
@Service
@Log4j2
@RequiredArgsConstructor
public class TaskMainRepository {

    public Page<TaskMain> pageList(RTaskMain.SearchBody search) {
        QTaskMain query = new QTaskMain();
        Long loginId = AclUtils.getLoginId();
        Opt.ofBlankAble(search.getAccountId()).ifPresent(x -> query.account.id.eq(x));
        Opt.ofBlankAble(search.getStaffId()).ifPresent(x -> query.staff.id.eq(x));
        Opt.ofBlankAble(search.getYourself()).ifPresent(x -> {
            if (x) {
                query.account.id.eq(loginId);
            }
        });
        query.order().id.desc();
        query.setFirstRow(search.firstRow()).setMaxRows(search.maxRows());
        return new Page<TaskMain>().load(query.findPagedList());
    }


    public void addModification(RTaskMain.AllocationBody body) {
        log.error(body);
        TaskMain one = new QTaskMain().id.eq(body.getId()).setMaxRows(1).findOne();
        Assert.isFalse(ObjUtil.isNull(one), "未查找到客服工单");
        SimpleAccount account = new QSimpleAccount().id.eq(body.getCode()).setMaxRows(1).findOne();
        if (ObjUtil.isNull(account)) {
            //下发入栏
            BasicStaffView staff = new QBasicStaffView().id.eq(body.getCode()).setMaxRows(1).findOne();
            Assert.isFalse(ObjUtil.isNull(staff), "未找到该管家");
            one.setStaff(staff).setRefreshExpiredTime(LocalDateTime.now());
            assign(one.getOrders().getId(), staff.getCode());

        } else {
            one.setAccount(account);
        }
        one.update();
    }

    public void assign(Long id, String code) {
        QPigOrder query = new QPigOrder();
        PigOrder record = query.id.eq(id).setMaxRows(1).findOne();
        Assert.isFalse(ObjUtil.isNull(record), ArcoTips.UPDATE_NOT_FOUND);
        record.setStaff(new BasicStaffView().setCode(code)).setStatus(PigOrderStatus.PROGRESS);
        Objects.requireNonNull(record).update();
    }


    public void update(Long id, RTaskMain.EditBody body) {


        log.warn("收到的ID是 = {}", id);
        log.warn("收到的数据是 = {}", body);
        QTaskMain query = new QTaskMain();
        TaskMain record = query.id.eq(id).setMaxRows(1).findOne();
        Assert.notNull(record, ArcoTips.UPDATE_NOT_FOUND);
        BeanUtil.copyProperties(body, record);
        Objects.requireNonNull(record).update();


    }


    public void addKind(Long oderId) {
        //创建数据
        List<TaskKind> list = new QTaskKind().findList();
        List<TasksList> lists = list.stream().map(x -> new TasksList()
                        .setCode(x.getCode())
                        .setTitle(x.getTitle())
                        .setContent(x.getContent())
                        .setIsComplete(false))
                .collect(Collectors.toList());
        TaskMain taskMain = new TaskMain()
                .setContent(lists)
                .setOrders(new OrderView().setId(oderId))
                .setIsEnter(false);
        taskMain.save();
    }

    public int delByIds(List<Long> ids) {
        List<TaskMain> list = new QTaskMain().id.in(ids).findList();
        Assert.notEmpty(list, ArcoTips.DELETE_NOT_FOUND);
        for (TaskMain module : list) {
            log.warn("检查数据是否允许被删除  {}", module);
        }
        return DB.deleteAll(list);


    }


    /**
     * 获取数据字典下拉选项
     *
     * @return {@code List<SelectOption>}
     */
//    public List<SelectOption> getOptions() {
//        return new QTaskMain().findStream()
//                .map(x -> new SelectOption().setLabel(x.getTitle()).setValue(x.getCode()).setDisabled(false))
//                .collect(Collectors.toList());
//    }


}
